package com.cunshe.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.StringJoiner;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseVO<T> implements Serializable {

    /**
     * 异常码
     */
    private Integer code;

    /**
     * 描述
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    public ResponseVO() {
    }

    public ResponseVO(Integer code, String msg) {
        this.code = code;
        this.message = msg;
    }

    public ResponseVO(Integer code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public static <T> ResponseVO<T> success() {
        return new ResponseVO(0, "success");
    }

    public static <T> ResponseVO<T> success(Object data) {
        return new ResponseVO(0, "success", data);
    }

    public static <T> ResponseVO<T> success(int code, String msg) {
        return new ResponseVO(code, msg);
    }

    public static <T> ResponseVO<T> error(int code, String msg) {
        return new ResponseVO(code, msg);
    }

    public static <T> ResponseVO<T> success(int code, String msg, T data) {
        return new ResponseVO(code, msg, data);
    }

    @JsonIgnore
    public boolean isError() {
        return !isSuccess();
    }

    @JsonIgnore
    public boolean isSuccess() {
        if (this.getCode() == 0) {
            return true;
        }
        return false;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", ResponseVO.class.getSimpleName() + "[", "]")
                .add("code=" + code)
                .add("message='" + message + "'")
                .add("data=" + data)
                .toString();
    }

}